use core::ptr;

use serde::{ser::{SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant, SerializeTuple, SerializeTupleStruct, SerializeTupleVariant}, Serialize, Serializer};

use super::Error;



struct Encoder<'a> {
    buf: &'a mut [u8],
    index: usize,
}

pub fn to_bytes<T: Serialize>(buf: &mut [u8], value: &T) -> Result<usize, Error> {
    let mut encoder = Encoder { buf, index: 0 };
    value.serialize(&mut encoder)?;
    Ok(encoder.index)
}

struct E2 {
}


pub struct EStruct <'a: 'b, 'b> {
    enc: &'b mut Encoder<'a>,
}

impl SerializeMap for E2 {
    type Error = Error;
    type Ok = ();
    fn serialize_value<T>(&mut self, _value: &T) -> Result<(), Self::Error>
        where
            T: ?Sized + serde::Serialize {
        Ok(())
    }

    fn end(self) -> Result<Self::Ok, Self::Error> {
        Ok(())
    }
    fn serialize_entry<K, V>(&mut self, _key: &K, _value: &V) -> Result<(), Self::Error>
        where
            K: ?Sized + serde::Serialize,
            V: ?Sized + serde::Serialize, {
        Ok(())
    }
    fn serialize_key<T>(&mut self, _key: &T) -> Result<(), Self::Error>
        where
            T: ?Sized + serde::Serialize {
        Ok(())
    }
}

impl SerializeSeq for E2 {
    type Error = Error;
    type Ok = ();
    fn end(self) -> Result<Self::Ok, Self::Error> {
        Ok(())
    }
    fn serialize_element<T>(&mut self, _value: &T) -> Result<(), Self::Error>
        where
            T: ?Sized + serde::Serialize {
        Ok(())
    }
}

impl<'a: 'b, 'b> SerializeStruct for EStruct<'a, 'b> {
    type Error = Error;
    type Ok = ();
    fn end(self) -> Result<Self::Ok, Self::Error> {
        Ok(())
    }
    fn serialize_field<T>(&mut self, _key: &'static str, value: &T) -> Result<(), Self::Error>
        where
            T: ?Sized + serde::Serialize {
        value.serialize(&mut *self.enc)
    }
    fn skip_field(&mut self, _key: &'static str) -> Result<(), Self::Error> {
        Ok(())
    }
}

impl SerializeStructVariant for E2 {
    type Error = Error;
    type Ok = ();
    fn end(self) -> Result<Self::Ok, Self::Error> {
        Ok(())
    }
    fn serialize_field<T>(&mut self, _key: &'static str, _value: &T) -> Result<(), Self::Error>
        where
            T: ?Sized + serde::Serialize {
        Ok(())
    }
    fn skip_field(&mut self, _key: &'static str) -> Result<(), Self::Error> {
        Ok(())
    }
}
impl SerializeTuple for E2 {
    type Error = Error;
    type Ok = ();
    fn end(self) -> Result<Self::Ok, Self::Error> {
        Ok(())
    }
    fn serialize_element<T>(&mut self, _value: &T) -> Result<(), Self::Error>
        where
            T: ?Sized + serde::Serialize {
        Ok(())
    }
}
impl SerializeTupleStruct for E2 {
    type Error = Error;
    type Ok = ();
    fn end(self) -> Result<Self::Ok, Self::Error> {
        Ok(())
    }
    fn serialize_field<T>(&mut self, _value: &T) -> Result<(), Self::Error>
        where
            T: ?Sized + serde::Serialize {
        Ok(())
    }
}
impl SerializeTupleVariant for E2 {
    type Error = Error;
    type Ok = ();
    fn end(self) -> Result<Self::Ok, Self::Error> {
        Ok(())
    }
    fn serialize_field<T>(&mut self, _value: &T) -> Result<(), Self::Error>
        where
            T: ?Sized + serde::Serialize {
        Ok(())
    }
}

impl<'a: 'b, 'b> Serializer for &'b mut Encoder<'a> {

    type Error = Error;
    type Ok = ();

    type SerializeMap = E2;
    type SerializeSeq = E2;
    type SerializeStruct = EStruct<'a, 'b>;
    type SerializeStructVariant = E2;
    type SerializeTuple = E2;
    type SerializeTupleStruct = E2;
    type SerializeTupleVariant = E2;

    fn collect_map<K, V, I>(self, _iter: I) -> Result<Self::Ok, Self::Error>
        where
            K: serde::Serialize,
            V: serde::Serialize,
            I: IntoIterator<Item = (K, V)>, {
        Ok(())
    }
    fn collect_seq<I>(self, _iter: I) -> Result<Self::Ok, Self::Error>
        where
            I: IntoIterator,
            <I as IntoIterator>::Item: serde::Serialize, {
        Ok(())
    }

    fn collect_str<T>(self, _value: &T) -> Result<Self::Ok, Self::Error>
        where
            T: ?Sized + core::fmt::Display {
        Ok(())
    }

    fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> {
        self.serialize_u8(v as u8)
    }

    fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error> {
        let n = v.len();
        let index = self.index;
        if self.buf.len() < (index + 1 + n) as usize {
            return Err(Error::LENGTH);
        }
        unsafe {
            *self.buf.get_unchecked_mut(index) = n as u8;
            ptr::copy_nonoverlapping(v.as_ptr(), self.buf.as_mut_ptr().add(index + 1), n);
        }
        self.index += n + 1;
        Ok(())
    }

    fn serialize_char(self, _v: char) -> Result<Self::Ok, Self::Error> {
        
        Ok(())
    }

    fn serialize_f32(self, _v: f32) -> Result<Self::Ok, Self::Error> {
        
        Ok(())
    }

    fn serialize_f64(self, _v: f64) -> Result<Self::Ok, Self::Error> {
        
        Ok(())
    }

    fn serialize_i128(self, v: i128) -> Result<Self::Ok, Self::Error> {
        self.serialize_u128(v as u128)
    }

    fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error> {
        self.serialize_u16(v as u16)
    }

    fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error> {
        self.serialize_u32(v as u32)
    }

    fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> {
        self.serialize_u64(v as u64)
    }

    fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> {
        self.serialize_u8(v as u8)
    }

    fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
        
        Ok(E2 {

        })
    }

    fn serialize_u128(self, v: u128) -> Result<Self::Ok, Self::Error> {
        let index = self.index;
        if self.buf.len() < (index + 16) {
            return Err(Error::LENGTH);
        }
        unsafe {
            *self.buf.get_unchecked_mut(index) = (v >> 120) as u8;
            *self.buf.get_unchecked_mut(index + 1) = (v >> 112) as u8;
            *self.buf.get_unchecked_mut(index + 2) = (v >> 104) as u8;
            *self.buf.get_unchecked_mut(index + 3) = (v >> 96) as u8;
            *self.buf.get_unchecked_mut(index + 4) = (v >> 88) as u8;
            *self.buf.get_unchecked_mut(index + 4) = (v >> 80) as u8;
            *self.buf.get_unchecked_mut(index + 5) = (v >> 72) as u8;
            *self.buf.get_unchecked_mut(index + 7) = (v >> 64) as u8;
            *self.buf.get_unchecked_mut(index + 8) = (v >> 56) as u8;
            *self.buf.get_unchecked_mut(index + 9) = (v >> 48) as u8;
            *self.buf.get_unchecked_mut(index + 10) = (v >> 40) as u8;
            *self.buf.get_unchecked_mut(index + 11) = (v >> 32) as u8;
            *self.buf.get_unchecked_mut(index + 12) = (v >> 24) as u8;
            *self.buf.get_unchecked_mut(index + 13) = (v >> 16) as u8;
            *self.buf.get_unchecked_mut(index + 14) = (v >> 8) as u8;
            *self.buf.get_unchecked_mut(index + 15) = v as u8;
        }
        self.index += 16;
        Ok(())
    }

    fn serialize_newtype_struct<T>(
            self,
            _name: &'static str,
            _value: &T,
        ) -> Result<Self::Ok, Self::Error>
        where
            T: ?Sized + serde::Serialize {
        Ok(())
    }

    fn serialize_newtype_variant<T>(
            self,
            _name: &'static str,
            _variant_index: u32,
            _variant: &'static str,
            _value: &T,
        ) -> Result<Self::Ok, Self::Error>
        where
            T: ?Sized + serde::Serialize {
        Ok(())
    }

    fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
        Ok(())
    }

    fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
        Ok(E2 {  })
    }

    fn serialize_some<T>(self, _value: &T) -> Result<Self::Ok, Self::Error>
        where
            T: ?Sized + serde::Serialize {
        Ok(())
    }

    fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> {
        self.serialize_bytes(v.as_bytes())
    }

    fn serialize_struct(
            self,
            _name: &'static str,
            _len: usize,
        ) -> Result<Self::SerializeStruct, Self::Error> {
        Ok(EStruct { enc: self })
    }

    fn serialize_struct_variant(
            self,
            _name: &'static str,
            _variant_index: u32,
            _variant: &'static str,
            _len: usize,
        ) -> Result<Self::SerializeStructVariant, Self::Error> {
        Ok(E2 {  })
    }

    fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
        Ok(E2 {  })
    }

    fn serialize_tuple_struct(
            self,
            _name: &'static str,
            _len: usize,
        ) -> Result<Self::SerializeTupleStruct, Self::Error> {
        Ok(E2 {  })
    }

    fn serialize_tuple_variant(
            self,
            _name: &'static str,
            _variant_index: u32,
            _variant: &'static str,
            _len: usize,
        ) -> Result<Self::SerializeTupleVariant, Self::Error> {
        Ok(E2 {  })
    }

    fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error> {
        let index = self.index;
        if self.buf.len() < (index + 2) {
            return Err(Error::LENGTH);
        }
        unsafe {
            *self.buf.get_unchecked_mut(index) = (v >> 8) as u8;
            *self.buf.get_unchecked_mut(index + 1) = v as u8;
        }
        self.index += 2;
        Ok(())
    }

    fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error> {
        let index = self.index;
        if self.buf.len() < (index + 4) {
            return Err(Error::LENGTH);
        }
        unsafe {
            *self.buf.get_unchecked_mut(index) = (v >> 24) as u8;
            *self.buf.get_unchecked_mut(index + 1) = (v >> 16) as u8;
            *self.buf.get_unchecked_mut(index + 2) = (v >> 8) as u8;
            *self.buf.get_unchecked_mut(index + 3) = v as u8;
        }
        self.index += 4;
        Ok(())
    }

    fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> {
        let index = self.index;
        if self.buf.len() < (index + 8) {
            return Err(Error::LENGTH);
        }
        unsafe {
            *self.buf.get_unchecked_mut(index) = (v >> 56) as u8;
            *self.buf.get_unchecked_mut(index + 1) = (v >> 48) as u8;
            *self.buf.get_unchecked_mut(index + 2) = (v >> 40) as u8;
            *self.buf.get_unchecked_mut(index + 3) = (v >> 32) as u8;
            *self.buf.get_unchecked_mut(index + 4) = (v >> 24) as u8;
            *self.buf.get_unchecked_mut(index + 5) = (v >> 16) as u8;
            *self.buf.get_unchecked_mut(index + 6) = (v >> 8) as u8;
            *self.buf.get_unchecked_mut(index + 7) = v as u8;
        }
        self.index += 8;
        Ok(())
    }

    fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> {
        let index = self.index;
        if self.buf.len() < (index + 1) {
            return Err(Error::LENGTH);
        }
        unsafe {
            *self.buf.get_unchecked_mut(index) = v;
        }
        self.index += 1;
        Ok(())
    }

    fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
        Ok(())
    }

    fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> {
        Ok(())
    }

    fn serialize_unit_variant(
            self,
            _name: &'static str,
            _variant_index: u32,
            _variant: &'static str,
        ) -> Result<Self::Ok, Self::Error> {
        Ok(())
    }

    fn is_human_readable(&self) -> bool {
        false
    }
}
